//四种视图状态
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/utils/utils.dart';

enum LoadState {
  State_Success,
  State_None,
  State_Error,
  State_Loading,
  State_Empty
}

///根据不同状态来展示不同的视图
class LoadStateLayout extends StatefulWidget {
  final Color bgColor; // 背景颜色
  final LoadState state; //页面状态
  final Widget successWidget; //成功视图
  final Widget loadingWidget; //自定义loading视图
  final Widget emptyWidget; //自定义空视图
  final String emptyStr;
  final Widget errorWidget; //自定义错误图
  final VoidCallback errorRetry; //错误事件处理
  final VoidCallback emptyRetry; //空数据事件处理

  LoadStateLayout(
      {Key key,
      this.state = LoadState.State_Loading, //默认为加载状态
      this.successWidget,
      this.loadingWidget,
      this.errorRetry,
      this.emptyRetry,
      this.emptyStr,
      this.bgColor,
      this.emptyWidget,
      this.errorWidget})
      : super(key: key);

  @override
  _LoadStateLayoutState createState() => _LoadStateLayoutState();
}

class _LoadStateLayoutState extends State<LoadStateLayout> {
  @override
  Widget build(BuildContext context) {
    return Container(
      //宽高都充满屏幕剩余空间
      width: double.infinity,
      height: double.infinity,
      color: widget.bgColor != null ? widget.bgColor : ThemeColors.colorType11,
      child: _buildWidget,
    );
  }

  ///根据不同状态来显示不同的视图
  Widget get _buildWidget {
    switch (widget.state) {
      case LoadState.State_Success:
        return widget.successWidget;
        break;
      case LoadState.State_None:
        return Container();
        break;
      case LoadState.State_Error:
        return _errorView;
        break;
      case LoadState.State_Loading:
        return _loadingView;
        break;
      case LoadState.State_Empty:
        return widget.emptyWidget != null ? widget.emptyWidget : _emptyView;
        break;
      default:
        return null;
    }
  }

  ///加载中视图
  Widget get _loadingView {
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      color: Colors.white,
      child: Container(
        height: 200,
        padding: EdgeInsets.all(10),
        child: widget.loadingWidget ??
            Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                Shimmer.fromColors(
                  baseColor: ThemeColors.colorType50,
                  highlightColor: ThemeColors.colorType49,
                  child: Text("努力加载中..",
                      style: TextStyle(
                          color: ThemeColors.colorType50,
                          fontSize: ScreenAdapter.sp(30))),
                )
              ],
            ),
      ),
    );
  }

  ///错误视图
  Widget get _errorView {
    return Container(
        width: double.infinity,
        height: double.infinity,
        alignment: Alignment.center,
        child: InkWell(
          onTap: widget.errorRetry,
          child: widget.errorWidget ??
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset(RPath.imgPath + 'img_empty.png',
                      width: ScreenAdapter.width(200),
                      height: ScreenAdapter.height(200)),
                  SizedBox(height: ScreenAdapter.height(30)),
                  Text(
                    "加载失败!",
                    style: TextStyle(
                        color: ThemeColors.colorType50,
                        fontSize: ScreenAdapter.sp(30)),
                  ),
                ],
              ),
        ));
  }

  ///Empty视图
  Widget get _emptyView {
    return Container(
        width: double.infinity,
        height: double.infinity,
        alignment: Alignment.center,
        child: InkWell(
          onTap: widget.emptyRetry,
          child: widget.emptyRetry ??
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset(RPath.imgPath + 'img_empty.png',
                      width: ScreenAdapter.width(200),
                      height: ScreenAdapter.height(200)),
                  SizedBox(height: ScreenAdapter.height(30)),
                  Text(
                    isEmpty(widget.emptyStr) ? "暂无相关数据!" : widget.emptyStr,
                    style: TextStyle(
                        color: ThemeColors.colorType50,
                        fontSize: ScreenAdapter.sp(30)),
                  ),
                ],
              ),
        ));
  }
}
